package com.wangjie;

/**
 * 懒汉式
 * 虽然达到了按需初始化的目的，却带来高并发不安全的问题
 */
public class Mgr02 {

    private static Mgr02 instance;

    private Mgr02() {
    }

    public static Mgr02 getInstance() {
        if (instance == null) {
            try {
                Thread.sleep(2000);
                instance = new Mgr02();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return instance;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Mgr02.getInstance().hashCode());
                }
            }).start();
        }
    }


}
